package com.lvyq.myplayer.ui.activity.collect

import android.content.Intent
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.lvyq.myplayer.R
import com.lvyq.myplayer.adapter.current.CurrentPlayLstAdapter
import com.lvyq.myplayer.base.BaseActivity
import com.lvyq.myplayer.common.shared.MainAudioSharedPrefsManager
import com.lvyq.myplayer.common.util.MockDataUtil
import com.lvyq.myplayer.service.AudioService
import com.lvyq.myplayer.ui.fragment.collect.SongCollectAlbumPagerFragment
import com.lvyq.myplayer.ui.fragment.collect.SongCollectPagerFragment
import com.lvyq.myplayer.ui.fragment.collect.SongCollectPlayLstPagerFragment
import com.lvyq.myplayer.ui.fragment.history.SongPlayPagerFragment
import com.lvyq.myplayer.util.ToolBarManager

/**
 * 文件描述：IndexCollectActivity 我的收藏
 * 作者：lvyq
 * 创建时间：2025-06-04
 * 修改时间：2025-06-04
 */
class IndexCollectActivity: BaseActivity() , ToolBarManager {

    override val toolbar by lazy { findViewById<Toolbar>(R.id.toolBar) }

    private lateinit var historyRecView: ViewPager2

    private lateinit var playHistoryTypeTab: TabLayout

    var playTypeList= MockDataUtil.playTypeList()

    override var currentPlayAdapter: CurrentPlayLstAdapter? = null
    override fun getLayoutId(): Int {
        return  R.layout.activity_play_history
    }

    override fun initData() {
        initPlayBoxService()
        historyRecView = findViewById(R.id.rvHistoryView)
        playHistoryTypeTab=findViewById(R.id.playHistoryTypeTab)

        //viewPage 绑定
        historyRecView.adapter=object : FragmentStateAdapter(this){
            override fun getItemCount(): Int {
                return playTypeList.size
            }

            override fun createFragment(position: Int): Fragment {
                //单曲
                if (position==0){
                    return SongCollectPagerFragment()
                }
                //歌单
                if (position==1){
                    return SongCollectPlayLstPagerFragment()
                }
                //专辑
                if (position==2){
                    return SongCollectAlbumPagerFragment()
                }
                return SongPlayPagerFragment()
            }

        }
        // 关联 TabLayout 和 ViewPager2
        TabLayoutMediator(playHistoryTypeTab, historyRecView) { tab, position ->
            tab.text = playTypeList[position].name.toString()
        }.attach()
        // 默认选中第一个页面
        historyRecView.setCurrentItem(0, true)
    }

    override fun initListener() {
        super.initListener()
        initSettingToolBar("我的收藏")
        enableBackButton()
    }
    /**
     * 播放栏服务初始化
     */
    private fun initPlayBoxService() {
        initMainPlayBoxListener()
        currentPlayAdapter=getCurrentPlayLstAdapter()
        initAudioPlayLst(this)

        //设置viewpage2 适配器
        recreateViewPagerAdapter()
        initMainPlayStateChangeUI()
        // updateMainPlayStateBtn(false)
        var songInfo = MainAudioSharedPrefsManager.getSongInfo(this)
        var currentAudioPosition = MainAudioSharedPrefsManager.getCurrentAudioPosition(this)
        //通过audioservice 播放,混合启动，保证退出当前页仍然播放
        val audioIntent = Intent(this, AudioService::class.java)
        //传递值到AudioService中
        audioIntent.putExtra("data",songInfo)
        audioIntent.putExtra("from",-1)
        audioIntent.putExtra("position",currentAudioPosition?.toInt())
        //绑定 BIND_AUTO_CREATE 服务未创建时创建并绑定
        bindService(audioIntent,connection, BIND_AUTO_CREATE)
        //启动
        startService(audioIntent)
    }

    private fun getCurrentPlayLstAdapter(): CurrentPlayLstAdapter {
        return CurrentPlayLstAdapter(this)
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}